package tech.aistar.day01;

/**
 * 本类用来演示: 交换俩个变量的值,但是不允许使用中间变量
 *
 * @author: success
 * @date: 2022/7/18 3:51 下午
 */
public class SwapDemo {
    public static void main(String[] args) {
        //相同为0,不同为1
        // a^b
        // 1 0 1 0
        // 1 0 0 0
        //--------
        // 0 0 1 0   => 2

        //10^8 = 2
        //10^8^8 =10

        //0 0 1 0
        //1 0 0 0
        //-------
        //1 0 1 0   => 10

        //结论 - 一个数连续异或同一个数俩次,结果是它本身

        int a = 10;
        int b = 8;

        a = a^b;//a = 10^8
        b = a^b;//b = 10^8^8 = 10
        a = a^b;//a = 10^8^10 = 8

        System.out.println(a);//8
        System.out.println(b);//10

        System.out.println("====");

        int m = 10;
        int n = 20;
        m = n^(n=m)^m;
        System.out.println(m);
        System.out.println(n);

    }

    public static void change(int a,int b){
        int temp = a;
        a = b;
        b =temp;

        System.out.println("a:"+a);
        System.out.println("b:"+b);
    }
}
